﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
using System.Text.RegularExpressions;

namespace dzs.Web.tenderExpert
{
    public partial class AddOrUpTenderFirmRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int tenderFirmRecordId = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(tenderFirmRecordId);
                }
            }
        }
        private void ShowInfo(int tenderFirmRecordId)
        {
            Maticsoft.BLL.tenderFirmRecord bll = new Maticsoft.BLL.tenderFirmRecord();
            Maticsoft.Model.tenderFirmRecord model = bll.GetModel(tenderFirmRecordId);
            this.lbltenderFirmRecordId.Text = model.tenderFirmRecordId.ToString();
            this.txtrecordName.Text = model.recordName;
            this.txtrecordContent.Text = model.recordContent;
            this.txtrecordScore.Text = model.recordScore.ToString();
            this.txttenderTime.Text = model.tenderTime.ToString();          
            this.txttimeCount.Text = model.timeCount.ToString();
            this.ddl_checkUser.SelectedValue = model.check_uid.Value.ToString();
            this.ddl_tenderFirm.SelectedValue = model.tenderFirmId.Value.ToString();

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                Regex re = new Regex(@"^(\+|-)?\d+($|\.\d+$)");
                DataSet ds = Session["login"] as DataSet;
                int uid = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());

                string strErr = "";
                if (this.txtrecordName.Text.Trim().Length == 0)
                {
                    strErr += "名称不能为空！\\n";
                }
                if (this.txtrecordContent.Text.Trim().Length == 0)
                {
                    strErr += "行为记录不能为空！\\n";
                }
                if (!re.IsMatch(this.txtrecordScore.Text))
                {
                    strErr += "奖惩分值格式错误,请填写数字，如果为整数，请加上.0,例：1.0\\n";
                }

                if (!PageValidate.IsNumber(txttimeCount.Text))
                {
                    strErr += "时限（单位：月）格式错误！\\n";
                }
                if (this.ddl_tenderFirm.SelectedValue == "-1")
                {
                    strErr += "请选择投标厂商名称！\\n";
                }
                if (this.ddl_checkUser.SelectedValue == "-1")
                {
                    strErr += "请选择审核人！\\n";
                }

                if (!string.IsNullOrEmpty(strErr))
                {
                    Maticsoft.Common.MessageBox.Show(this, strErr);
                    return;
                }

                string recordName = this.txtrecordName.Text;
                string recordContent = this.txtrecordContent.Text;
                decimal recordScore = decimal.Parse(this.txtrecordScore.Text);
                DateTime tenderTime = DateTime.Parse(this.txttenderTime.Text);
                decimal tenderScore = 0;
                int timeCount = int.Parse(this.txttimeCount.Text);
                int tenderFirmId = int.Parse(this.ddl_tenderFirm.SelectedValue);
                int check_uid = int.Parse(this.ddl_checkUser.SelectedValue);
                int check_status = 0;
                //int uid = ;

                Maticsoft.Model.tenderFirmRecord model = new Maticsoft.Model.tenderFirmRecord();
                model.recordName = recordName;
                model.recordContent = recordContent;
                model.recordScore = recordScore;
                model.tenderTime = tenderTime;
                model.tenderScore = tenderScore;
                model.timeCount = timeCount;
                model.tenderFirmId = tenderFirmId;
                model.check_uid = check_uid;
                model.check_status = check_status;
                model.uid = uid;

                Maticsoft.BLL.tenderFirmRecord bll = new Maticsoft.BLL.tenderFirmRecord();

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    model.tenderFirmRecordId = (Convert.ToInt32(Request.Params["id"]));
                    if (bll.Update(model))
                    {
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListTenderFirmRecord.aspx");
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "保存失败！");
                    }
                }
                else
                {
                    if (bll.Add(model) > 0)
                    {
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListTenderFirmRecord.aspx");
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "保存失败！");
                    }
                }
               
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "登录信息已过期，请重新登录！");
                //Maticsoft.Common.MessageBox.ResponseScript(this, "top.location.href='../../Default.aspx'");
            }
        }

        private void BindDDL() 
        {
            DataSet ds_ld = new dzs.BLL.user().GetList(" roles_id =16 ");//领导的角色ID这里设置为16

            this.ddl_checkUser.DataSource = ds_ld;
            this.ddl_checkUser.DataTextField = "user_name";
            this.ddl_checkUser.DataValueField = "user_id";
            this.ddl_checkUser.DataBind();

            this.ddl_checkUser.Items.Insert(0, new ListItem("--未选择--","-1"));

            DataSet ds_tender = new Maticsoft.BLL.tenderFirm().GetList("");

            this.ddl_tenderFirm.DataSource = ds_tender;
            this.ddl_tenderFirm.DataTextField = "tenderFirmName";
            this.ddl_tenderFirm.DataValueField = "tenderFirmId";

            this.ddl_tenderFirm.DataBind();
            this.ddl_tenderFirm.Items.Insert(0, new ListItem("--未选择--", "-1"));
        
        }
    }
}